package com.roshan.web;

import com.roshan.domain.Employee;
import com.roshan.domain.Rsvp;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.support.FormattingConversionServiceFactoryBean;

/**
 * A central place to register application converters and formatters.
 */
public class ApplicationConversionServiceFactoryBean extends FormattingConversionServiceFactoryBean {

    @Override
    protected void installFormatters(FormatterRegistry registry) {
        super.installFormatters(registry);
        // Register application converters and formatters
    }

    public void installLabelConverters(FormatterRegistry registry) {
        registry.addConverter(new EmployeeConverter());
        registry.addConverter(new RsvpConverter());
    }

    public void afterPropertiesSet() {
        super.afterPropertiesSet();
        installLabelConverters(getObject());
    }

    static class EmployeeConverter implements Converter<Employee, String> {
        public String convert(Employee employee) {
            return new StringBuilder().append(employee.getFirstName()).append(" ").append(employee.getCode())
                    .append(" ").append(employee.getEmail()).append(" ").append(employee.getAttending()).toString();
        }

    }

    static class RsvpConverter implements Converter<Rsvp, String> {
        public String convert(Rsvp rsvp) {
            return new StringBuilder().append(rsvp.getCode()).append(" ").append(rsvp.getEmail()).append(" ")
                    .append(rsvp.getAttending()).append(" ").append(rsvp.getSpecialRequests()).toString();
        }

    }
}
